/*
 * js 伪静态类
 * @author:libaoling
 * @time : 2015-1-16
 */
var Rewrite = {
    rewrite_maps:{
        place_category_cate_id_order : 'place_category_category_order',
        place_category_cate_id_price : 'place_category_category_price',
        place_category_cate_id_start_stock : 'place_category_category_start_stock',
        place_category_cate_id_order_price : 'place_category_category_order_price',
        place_category_cate_id_order_start_stock : 'place_category_category_order_start_stock',
        place_category_cate_id_price_start_stock : 'place_category_category_price_start_stock',
        place_category_cate_id_order_price_start_stock : 'place_category_category_order_price_start_stock',
    },
    rewrite_rules :{
        place_category_category_order : {rewrite : 'place_category/cate_id/%cate_id%/order/%order%.html'},
        place_category_category_price : {rewrite : 'place_category/cate_id/%cate_id%/price/%price%.html'},
        place_category_category_start_stock : {rewrite : 'place_category/cate_id/%cate_id%/start_stock/%start_stock%.html'},
        place_category_category_order_price : {rewrite : 'place_category/cate_id/%cate_id%/order/%order%/price/%price%.html'},
        place_category_category_order_start_stock : {rewrite : 'place_category/cate_id/%cate_id%/order/%order%/start_stock/%start_stock%.html'},
        place_category_category_price_start_stock : {rewrite : 'place_category/cate_id/%cate_id%/price/%price%/start_stock/%start_stock%.html'},
        place_category_category_order_price_start_stock : {rewrite : 'place_category/cate_id/%cate_id%/order/%order%/price/%price%/start_stock/%start_stock%.html'},
    },
    
    get: function(query){
        var rewrite;
        if(!query){
            return;
        }
        
        var url_params = this._get_params(query);
        var rewrite_name = this._get_rule_by_param(url_params);
        var rewrite_url = this._get_rule(rewrite_name);
        
        if(rewrite_url){
            var patterns = this._get_replace_pattern(url_params);
            rewrite = rewrite_url.rewrite;
            
            for(var key in patterns){
                rewrite = rewrite.replace(patterns[key], url_params[key]);
            }
        }else{
            return;
        }
        
        return rewrite;
    },
    
    temp_get: function(query){
        var rewrite;
        query = query.replace('app=', '');
        query = query.replace(/\&|\=/g, '/');
        rewrite = query + ".html";
        
        return rewrite;
    },
    
    _get_params: function(query_string){
        var result = new Array();
        
        if(query_string){
            var tmp = query_string.split('&');
            for(var tmp_item in tmp){
                var q = tmp[tmp_item].split('=');
                result[q[0]] = q[1];
            }
        }
        return result;
    },
    
    _get_rule: function(rule_name){
        return this.rewrite_rules.hasOwnProperty(rule_name) ? this.rewrite_rules[rule_name] : '';
    },
    
    _get_rule_by_param: function(url_params){
        var key = this._get_mapkey(url_params);
        return this._get_rule_by_mapkey(key);
    },
    
    _get_mapkey: function(url_params){
        var key;
        var query = '';
        var app = url_params.hasOwnProperty('app') ? url_params['app'] : '';
        delete url_params['app'];
        if(url_params){
            for(var key in url_params){
                query += "_"+key;
            }
        }
        if(app){
            key = query ? (app + query) : '';
            
        }else{
            key = query;
        }
        return key;
    },
    
    _get_rule_by_mapkey: function(key){
        return this.rewrite_maps.hasOwnProperty(key) ? this.rewrite_maps[key] : '';
    },
    
    _get_replace_pattern: function(url_params){
        var result = new Array();
        
        if(url_params){
            for(var key in url_params){
                result[key] = '%'+key+'%';
            }
        }
        
        return result;
    }
}

